#ifndef FILEOUT_H_
#define FILEOUT_H_

#include "./Constants.h"
#include "./Parameter.h"
#include "./Gaussian.h"

namespace Fileout
{    
    //输出一维数据
    void fileout_1d_array(ofstream& output, double* array, int array_length, double factor){
        if (output.is_open())
        {
            for (int i = 0; i < array_length; i++)
            {
                output << array[i] * factor << " ";
            }
            output<< std::endl;
        }
    }
    //输出grid
    void fileout_grid(ofstream& output, double* f_grid, double* t_grid, 
                double* r_grid, double* z_grid, double* mask,
                int nt, int nr, int nz, int mask_len){
        fileout_1d_array(output, f_grid, nt, 1.);
        fileout_1d_array(output, t_grid, nt, Constants::au_fs);
        fileout_1d_array(output, r_grid, nr, Constants::au_nm * 0.001);
        fileout_1d_array(output, z_grid, nz, Constants::au_nm * 0.001);
        fileout_1d_array(output, mask,  mask_len, 1.);
    }

    //输出光强解析分布
    void output_Erz_accuracy(std::string filename, GaussianDual& pulse, double* R_grid, double* Z_grid, int nr, int nz){
        ofstream Erz_accuracy(filename+".txt");
        ofstream Er0z_accuracy(filename+"_r0.txt");

        if (Erz_accuracy)
        {
            for (int i = 0; i < nz; i++)
            {
                double zi = Z_grid[i];
                for (int j = 0; j < nr; j++)
                {
                    double rj = R_grid[j];
                    double amplitude = pulse.Intensity(zi, rj);
                    Erz_accuracy << amplitude << " ";

                    if (j == 0)
                    {
                        Er0z_accuracy << amplitude << " ";
                    }
                }
                Erz_accuracy << endl;
            }
        }

    }
    
    //全空间，输出Erza，频域电场，空间分布，某频率电场幅值E(r,w=n,z)(apm)
    void output_Erza(ofstream &fileout, int array_length, complex *array)
    {

        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double amplitude = fabs(array[index]) / (array_length / 2.);
                fileout << amplitude << " ";
            }
            fileout << endl;
        }
    }
    void outputErzaDual(ofstream &fileout, int array_length, complex *array1, complex *array2)
    {
        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double amplitude = fabs(array1[index]) / (array_length / 2.);
                amplitude += fabs(array2[index]) / (array_length / 2.);
                fileout << amplitude << " ";
            }
            fileout << endl;
        }
    }

    //全空间，输出Erzc，频域电场，空间分布，某频率电场(实部，虚部)E(r,w=n,z)(real, imag)
    void output_Erzc(ofstream &fileout, int array_length, complex *array)
    {
        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double real1 = array[index].real();
                double imag1 = array[index].imag();
                fileout << real1 << "+" << imag1 << "j" << " ";
            }
            fileout << endl;
        }
    }


    //全时间，输出Etz，时域电场，时空分布，轴向电场，E(r=0,t,z)
    void output_Etz(ofstream &fileout, int array_length, double *array)
    {
        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double amplitude = array[index];
                fileout << amplitude << " ";
            }
            fileout << endl;
        }
    }

    //全频率，输出Ewza, 频域空间分布，轴向电场幅值，E(r=0,w,z)(amp)
    void output_Ewza(ofstream &fileout, int array_length, complex *array)
    {

        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double amplitude = fabs(array[index]) / (array_length / 2.);
                fileout << amplitude << " ";
            }
            fileout << endl;
        }
    }

    //全频率，输出Ewzc, 频域空间分布，轴向电场(实部，虚部)E(r=0,w,z)(real, imag)
    void output_Ewzc(ofstream &fileout, int array_length, complex *array)
    {
        if (fileout)
        {
            for (int index = 0; index < array_length; index++)
            {
                double real1 = array[index].real();
                double imag1 = array[index].imag();
                fileout << real1 << "+" << imag1 << "j" << " ";
            }
            fileout << endl;
        }
    }

    //输出初始电场
    void ouput2DList(string filename, int row, int column, double **list)
    {
        ofstream output;
        output.open(filename);
        if (output)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    output << list[row][column] << " ";
                }
                output << endl;
            }
        }
    }

    // 输出processed_data_array
    //  void processed_data_array_d(ofstream& output, double* array, int array_length, double factor){
    //     if (output){
    //         for (int i = 0; i < array_length; i++)
    //         {
    //             output << array[i] * factor << " ";
    //         }
    //         output << std::endl;
    //     }
    // }
    void processed_data_array_c(ofstream &output, complex *array, int array_length, double factor)
    {
        if (output)
        {
            for (int index = 0; index < array_length; index++)
            {
                double real1 = array[index].real() * factor;
                double imag1 = array[index].imag() * factor;
                output << real1 << "+" << imag1 << "j" << " ";
            }
            output << endl;
        }
    }

} // namespace Fileout


#endif